{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--BOOK_INFORMATION-->\n",
    "<img align=\"left\" style=\"padding-right:10px;\" src=\"figures/PHydro-cover-small.png\">\n",
    "*This is the Jupyter notebook version of the [Python in Hydrology](http://www.greenteapress.com/pythonhydro/pythonhydro.html) by Sat Kumar Tomer.*\n",
    "*Source code is available at [code.google.com](https://code.google.com/archive/p/python-in-hydrology/source).*\n",
    "\n",
    "*The book is available under the [GNU Free Documentation License](http://www.gnu.org/copyleft/fdl.html). If you have comments, corrections or suggestions, please send email to satkumartomer@gmail.com.*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--NAVIGATION-->\n",
    "< [Kriging](10.03-Kriging.ipynb) | [Contents](Index.ipynb) | [PCA](10.05-PCA.ipynb)>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 10.4 偏差校正\n",
    "\n",
    "CDF匹配是水文数据中最常用于偏差校正的方法。对于一个水文变量x，该方法写成，\n",
    "\n",
    "<center>$x_{m-adjusted}=F^{-1}_{o-c}(F_{m-c}(x_{m-p})),\\quad\\quad\\quad\\quad\\quad(10.3)$</center>\n",
    "\n",
    "其中，下标m表示model，o表示观测，c表示现在，p表示预测，p是CDF。\n",
    "\n",
    "我们使用`ambhas.stas`库中的`bias_function`函数进行偏差校正。我们从导入该库开始。然后我们生成一些随机数用于偏差校正。我们故意修改模拟的变量均值，以至于偏差校正方法可以对比。正如我们看见的，模拟的预测值均值为2，然而观测数据在应用偏差校正前的均值接近于0。偏差调整模型的预测在应用基于偏差校正的CDF后，均值接近于0。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from ambhas.stats import bias_correction\n",
    "oc = np.random.randn(100)\n",
    "mc = 2+np.random.randn(100)\n",
    "mp = 2+np.random.randn(1000)\n",
    "\n",
    "print(\"mean of observed current is %f\"%oc.mean())\n",
    "print(\"mean of modeled current is %f\"%mc.mean())\n",
    "print(\"mean of modeled prediction is %f\"%mp.mean())\n",
    "\n",
    "mp_adjusted = bias_correction(oc, mc, mp)\n",
    "print(\"mean of adjusted modeled prediction is %f\"%mp_adjusted.mean())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
